package com.team9.fitness.controller;

import com.team9.fitness.service.PostFavoriteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 帖子喜欢控制器
 * 
 * 提供帖子喜欢相关的REST API：
 * - 喜欢帖子
 * - 取消喜欢帖子
 * - 切换喜欢状态
 * - 查询喜欢状态和数量
 */
@Slf4j
@RestController
@RequestMapping("/api/post-favorite")
@CrossOrigin(origins = { "*" }, allowCredentials = "false")
public class PostFavoriteController {

    @Autowired
    private PostFavoriteService postFavoriteService;

    //
    // /**
    // * 喜欢帖子
    // */
    // @PostMapping("/favorite")
    // public ResponseEntity<Map<String, Object>> favoritePost(@RequestParam Long
    // userId, @RequestParam Long postId) {
    // Map<String, Object> response = new HashMap<>();
    //
    // try {
    // log.info("用户喜欢帖子: userId={}, postId={}", userId, postId);
    //
    // boolean success = postFavoriteService.favoritePost(userId, postId);
    //
    // if (success) {
    // response.put("success", true);
    // response.put("message", "喜欢成功");
    // response.put("favoriteCount",
    // postFavoriteService.getPostFavoriteCount(postId));
    // } else {
    // response.put("success", false);
    // response.put("message", "喜欢失败，可能已经喜欢过");
    // }
    //
    // return ResponseEntity.ok(response);
    // } catch (Exception e) {
    // log.error("喜欢帖子异常", e);
    // response.put("success", false);
    // response.put("message", "喜欢失败：" + e.getMessage());
    // return ResponseEntity.ok(response);
    // }
    // }
    //
    // /**
    // * 取消喜欢帖子
    // */
    // @PostMapping("/unfavorite")
    // public ResponseEntity<Map<String, Object>> unfavoritePost(@RequestParam Long
    // userId, @RequestParam Long postId) {
    // Map<String, Object> response = new HashMap<>();
    //
    // try {
    // log.info("用户取消喜欢帖子: userId={}, postId={}", userId, postId);
    //
    // boolean success = postFavoriteService.unfavoritePost(userId, postId);
    //
    // if (success) {
    // response.put("success", true);
    // response.put("message", "取消喜欢成功");
    // response.put("favoriteCount",
    // postFavoriteService.getPostFavoriteCount(postId));
    // } else {
    // response.put("success", false);
    // response.put("message", "取消喜欢失败，可能未喜欢过");
    // }
    //
    // return ResponseEntity.ok(response);
    // } catch (Exception e) {
    // log.error("取消喜欢帖子异常", e);
    // response.put("success", false);
    // response.put("message", "取消喜欢失败：" + e.getMessage());
    // return ResponseEntity.ok(response);
    // }
    // }

    /**
     * 切换帖子喜欢状态
     */
    @PostMapping("/toggle")
    public ResponseEntity<Map<String, Object>> togglePostFavorite(@RequestParam Long userId,
            @RequestParam Long postId) {
        Map<String, Object> response = new HashMap<>();

        try {
            log.info("用户切换帖子喜欢状态: userId={}, postId={}", userId, postId);

            boolean success = postFavoriteService.togglePostFavorite(userId, postId);

            if (success) {
                Boolean isFavorited = postFavoriteService.getUserFavoriteStatus(userId, postId);
                response.put("success", true);
                response.put("message", isFavorited ? "喜欢成功" : "取消喜欢成功");
                response.put("isFavorited", isFavorited);
                response.put("favoriteCount", postFavoriteService.getPostFavoriteCount(postId));
            } else {
                response.put("success", false);
                response.put("message", "操作失败");
            }

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("切换帖子喜欢状态异常", e);
            response.put("success", false);
            response.put("message", "操作失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 获取用户喜欢状态
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getFavoriteStatus(@RequestParam Long userId, @RequestParam Long postId) {
        Map<String, Object> response = new HashMap<>();

        try {
            Boolean isFavorited = postFavoriteService.getUserFavoriteStatus(userId, postId);
            int favoriteCount = postFavoriteService.getPostFavoriteCount(postId);

            response.put("success", true);
            response.put("isFavorited", isFavorited);
            response.put("favoriteCount", favoriteCount);

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取喜欢状态异常", e);
            response.put("success", false);
            response.put("message", "获取状态失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 获取帖子喜欢数量
     */
    @GetMapping("/count/{postId}")
    public ResponseEntity<Map<String, Object>> getFavoriteCount(@PathVariable Long postId) {
        Map<String, Object> response = new HashMap<>();

        try {
            int favoriteCount = postFavoriteService.getPostFavoriteCount(postId);

            response.put("success", true);
            response.put("favoriteCount", favoriteCount);

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取喜欢数量异常", e);
            response.put("success", false);
            response.put("message", "获取数量失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
}
